package com.ddwl.goods.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.schema.bo.order.LockStockBo;
import com.ddwl.schema.bo.order.QuerySkuPriceBo;
import com.ddwl.schema.vo.goods.SkuBestPriceVo;
import com.ddwl.schema.bo.order.ChangeStockBo;
import com.ddwl.goods.service.SkuService;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.SkuBo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.goods.SkuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

/**
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/8/28 16:55
 */
@Api(tags = "子商品服务")
@RequestMapping("/sku")
@RestController
public class SkuController extends BaseController {

    @Autowired
    private SkuService skuService;

    @PostMapping("index")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.SHOP_CONSOLE})
    @ApiOperation("分页查询子商品")
    public RestVo<List<SkuVo>> index(@Valid @RequestBody PageBo<SkuBo> pageBo) {
        return RestVo.ok(skuService.index(pageBo));
    }

    @DeleteMapping("del")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    @ApiOperation("删除子商品")
    public RestVo delete(@RequestParam String id) {
        skuService.delete(id);
        return RestVo.ok();
    }

    @PostMapping("updateByBatch")
    @ApiOperation("批量更新子商品库存")
    public RestVo updateByBatch(@RequestBody List<SkuBo> skubo) {
        skuService.updateByBatch(skubo);
        return RestVo.ok();
    }

    @PostMapping("updateStockAndPrice")
    @ApiOperation("批量更新子商品库存和价格")
    public RestVo updateStockAndPrice(@RequestBody List<SkuBo> skubo) {
        skuService.updateStockAndPrice(skubo);
        return RestVo.ok();
    }


    @PostMapping("changeStock")
    @ApiOperation("变更库存")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo changeStock(@RequestBody ChangeStockBo changeStockBo){
        skuService.changeStock(changeStockBo);
        return RestVo.ok();
    }

    /**
     * 变更销量
     */
    @PostMapping("changeSales")
    @ApiOperation("变更销量")
    public RestVo changeSales(@RequestBody ChangeStockBo changeStockBo){
        skuService.changeSales(changeStockBo);
        return RestVo.ok();
    };

    @PostMapping("checkLockStock")
    @ApiOperation("校验锁定库存")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo checkLockStock(@RequestBody LockStockBo lockStockBo){
        String s = skuService.checkLockStock(lockStockBo);
        RestVo restVo = RestVo.ok();
        restVo.setMsg(s);
        return restVo;
    }

    @PostMapping("lockStock")
    @ApiOperation("锁定库存")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo lockStock(@RequestBody LockStockBo lockStockBo){
        skuService.lockStock(lockStockBo);
        return RestVo.ok();
    }

    @GetMapping("/detail")
    @Auth(product = BaseEnum.ProductEnum.API)
    @ApiOperation("子商品详情")
    public RestVo<SkuVo> detail(@ApiParam(value = "子商品ID", required = true) @RequestParam String id) {
        return RestVo.ok(skuService.detail(id));
    }

    @GetMapping("/detailNew")
    @Auth(product = BaseEnum.ProductEnum.API)
    @ApiOperation("子商品详情withbarcode")
    public RestVo<SkuVo> detailWithBarcode(@ApiParam(value = "子商品barcode", required = true) @RequestParam String barcode) {
        return RestVo.ok(skuService.detailWithBarcode(barcode));
    }

    @PostMapping("/querySkuPrice")
    @ApiOperation("商品询价接口")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<List<SkuBestPriceVo>> enquiry(@RequestBody QuerySkuPriceBo querySkuPriceBo) {
        return RestVo.ok(skuService.querySkuPrice(querySkuPriceBo));
    }

    @PostMapping("/querySkuByBacodes")
    @ApiOperation("根据商品编码")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<List<SkuVo>> querySkuByBacodes(@RequestBody List<String> barcodes) {
        return RestVo.ok(skuService.getByBarCodeList(barcodes));
    }

    @PostMapping("/skuIds")
    @ApiOperation("查询商品下所有skuId")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<List<String>> querySkuIds(@RequestBody Collection<String> itemIds) {
        return RestVo.ok(skuService.querySkuIds(itemIds));
    }



}
